package com.lishe.lishe.lishe.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

/**
 * Created by shino on 2017/9/5.
 */

public class PermissionUtils {
    public static final int request_permission_camera=1001;
    public static final int request_WRITE_EXTERNAL_STORAGE=1002;

    private static final String[] permission_WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final String[] permission_CAMERA = {Manifest.permission.CAMERA};
    private static final String[] permission_CAMERA_AND_WRITE_EXTERNAL_STORAGE = {Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**
     * sd卡的写权限
     *
     * @return
     */
    public static boolean isStorageWritePermission(Context context) {
        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(context, permission_WRITE_EXTERNAL_STORAGE[0]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            if (i != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                Log.e("获取权限","qwe");
                ActivityCompat.requestPermissions((Activity) context, permission_WRITE_EXTERNAL_STORAGE,request_WRITE_EXTERNAL_STORAGE);
                return false;
            }
        }
        return true;
    }

    /**
     * 摄像头和sd卡存储权限
     *
     * @return
     */
    public static boolean isCameraAndWriteExternalStoragePermission(Context context) {
        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(context, permission_CAMERA[0]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            if (i != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                Log.e("获取权限","qwe");
                ActivityCompat.requestPermissions((Activity) context, permission_CAMERA_AND_WRITE_EXTERNAL_STORAGE,request_permission_camera);
                return false;
            }
        }
        return true;
    }

    /**
     * 摄像头权限
     *
     * @return
     */
    public static boolean isCameraPermission(Context context) {
        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(context, permission_CAMERA[0]);
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            if (i != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                Log.e("获取权限","qwe");
                ActivityCompat.requestPermissions((Activity) context, permission_CAMERA,request_permission_camera);
                return false;
            }
        }
        return true;
    }
}
